using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using IfacesEnumsStructsClasses;

namespace PageActivityMonitor
{
    public partial class Form1 : Form
    {

        #region Form
        public Form1()
        {
            InitializeComponent();

            this.Load += new EventHandler(Form1_Load);
            this.button1.Click += new EventHandler(button1_Click);
            this.button2.Click +=new EventHandler(button2_Click);

            this.cEXWB1.BeforeNavigate2 += new csExWB.BeforeNavigate2EventHandler(cEXWB1_BeforeNavigate2);
            this.cEXWB1.DocumentComplete += new csExWB.DocumentCompleteEventHandler(cEXWB1_DocumentComplete);

            this.cEXWB1.ProtocolHandlerBeginTransaction += new csExWB.ProtocolHandlerBeginTransactionEventHandler(cEXWB1_ProtocolHandlerBeginTransaction);
            this.cEXWB1.ProtocolHandlerOnResponse += new csExWB.ProtocolHandlerOnResponseEventHandler(cEXWB1_ProtocolHandlerOnResponse);
            this.cEXWB1.ProtocolHandlerDataFullyAvailable += new csExWB.ProtocolHandlerDataFullyAvailableEventHandler(cEXWB1_ProtocolHandlerDataFullyAvailable);
            this.cEXWB1.ProtocolHandlerDataFullyRead += new csExWB.ProtocolHandlerDataFullyReadEventHandler(cEXWB1_ProtocolHandlerDataFullyRead);
            this.cEXWB1.ProtocolHandlerOperationFailed += new csExWB.ProtocolHandlerOperationFailedEventHandler(cEXWB1_ProtocolHandlerOperationFailed);

            this.cEXWB1.WBOnDocumentChanged += new EventHandler(cEXWB1_WBOnDocumentChanged);

            this.cEXWB1.WBTopLevelOnLoad += new csExWB.TopLevelOnloadEventHandler(cEXWB1_WBTopLevelOnLoad);
            this.cEXWB1.WBTopLevelOnUnload += new csExWB.TopLevelOnUnloadEventHandler(cEXWB1_WBTopLevelOnUnload);
            this.cEXWB1.WBSecondaryOnLoad += new csExWB.SecondaryOnloadEventHandler(cEXWB1_WBSecondaryOnLoad);
            this.cEXWB1.WBSecondaryOnUnload += new csExWB.SecondaryOnUnloadEventHandler(cEXWB1_WBSecondaryOnUnload);
        }

        void button1_Click(object sender, EventArgs e)
        {
            this.cEXWB1.Navigate(this.comboBox1.Text);
        }

        void button2_Click(object sender, EventArgs e)
        {
            this.cEXWB1.Stop();
        }

        void Form1_Load(object sender, EventArgs e)
        {
            this.cEXWB1.NavToBlank();
        } 
        #endregion

        #region Webbrowser

        //Fired to indicate when a response from a server has been received
        void cEXWB1_ProtocolHandlerOnResponse(object sender, csExWB.ProtocolHandlerOnResponseEventArgs e)
        {
            Debug.Print(">>>>>>ProtocolHandlerOnResponse=> " + e.URL);
                //+ "\r\nResponseHeaders >>\r\n" + e.ResponseHeaders);
        }

        //Fired to indicate when a request for a resource is about to be initiated
        void cEXWB1_ProtocolHandlerBeginTransaction(object sender, csExWB.ProtocolHandlerBeginTransactionEventArgs e)
        {
            Debug.Print(">>>>>>ProtocolHandlerBeginTransaction=> " + e.URL);
                //+ "\r\nRequestHeaders >>\r\n" + e.RequestHeaders);
        }

        //Fired to indicate when a resource has been fully read by the MSHTML
        void cEXWB1_ProtocolHandlerDataFullyRead(object sender, csExWB.ProtocolHandlerDataFullyReadEventArgs e)
        {
            Debug.Print(">>>>>>ProtocolHandlerDataFullyRead=> " + e.URL);
        }

        //Fired to indicate when a resource has been fully downloaded and ready to be read by MSHTML
        void cEXWB1_ProtocolHandlerDataFullyAvailable(object sender, csExWB.ProtocolHandlerDataFullyAvailableEventArgs e)
        {
            Debug.Print(">>>>>>ProtocolHandlerDataFullyAvailable=> " + e.URL);
        }

        //Fired to indicate when download of a resource has failed
        void cEXWB1_ProtocolHandlerOperationFailed(object sender, csExWB.ProtocolHandlerOperationFailedEventArgs e)
        {
            Debug.Print(">>>>>>ProtocolHandlerOperationFailed=> " + e.URL);
        }

        //Fired to indicate (most of the time) when DOM has changed
        //Tested against AJAX pages
        void cEXWB1_WBOnDocumentChanged(object sender, EventArgs e)
        {
            try
            {
                Debug.Print(">>>>>>WBOnDocumentChanged=>");
            }
            catch (System.ObjectDisposedException)
            {
                Debug.Print("WBOnDocumentChanged::System.ObjectDisposedException");
            }
        }

        //Fired to indicate when a frame or iframe document has unloaded
        void cEXWB1_WBSecondaryOnUnload(object sender, csExWB.SecondaryOnloadEventArgs e)
        {
            Debug.Print(">>>>>>WBSecondaryOnUnload=>" + e.WebBrowser.LocationURL);
        }

        //Fired to indicate when a frame or iframe document has loaded
        void cEXWB1_WBSecondaryOnLoad(object sender, csExWB.SecondaryOnloadEventArgs e)
        {
            Debug.Print(">>>>>>WBSecondaryOnLoad=>" + e.WebBrowser.LocationURL);
        }

        //Fired to indicate when the top level document has loaded
        void cEXWB1_WBTopLevelOnUnload(object sender, CancelEventArgs e)
        {
            Debug.Print(">>>>>>WBTopLevelOnUnload=>" + this.cEXWB1.LocationUrl);
        }

        //Fired to indicate when the top level document has unloaded
        void cEXWB1_WBTopLevelOnLoad(object sender, CancelEventArgs e)
        {
            Debug.Print(">>>>>>WBTopLevelOnLoad=>" + this.cEXWB1.LocationUrl);
        }

        void cEXWB1_DocumentComplete(object sender, csExWB.DocumentCompleteEventArgs e)
        {
            Debug.Print(">>>>>>DocumentComplete=>" + e.url);
        }

        void cEXWB1_BeforeNavigate2(object sender, csExWB.BeforeNavigate2EventArgs e)
        {
            Debug.Print(">>>>>>BeforeNavigate2=>" + e.url);
        }

        #endregion


    }
}